clear all
set more off
cd "[working directory]\Replication_files"
global control "age hhmale high_edu minor  c4 c5 famsize   poorrg"
global FE "i.countyid"
use Household_data_anonymized.dta, clear

	do Define_bandwidth

	
	*** First stage
	xi:qui: reg treated  eligible runningrl runningrl_inter $control $FE	if runningrl<2 & runningrl>-2, robust
	outreg2 using ".\Output\Table2-4-panelB.xls", replace  lab keep(eligible) nocons bd(3) sd(3) rd(3) cttop(First)
		
	
********************************************************************************
	*** Table 2 and Table 3
********************************************************************************
	foreach var of varlist growth  ccei cceid {
	
		capture drop optimal_bd1
		g optimal_bd1 = (runningrl<`var'_optimal_bd & runningrl>-`var'_optimal_bd)
		
		
		global outreg2_options "lab keep(treated) sortvar(treated eligible) nocons bd(3) sd(3) rd(3) ad(3) adds(Dependent variable mean, r(mean), Percentage difference, percent, Bandwidth, `var'_optimal_bd)"
	
	
		*** Optimal bandwidth, no additional control
		xi:qui: ivregress 2sls `var'  (treated=eligible) runningrl runningrl_inter if optimal_bd1, robust	
		sum `var' if e(sample), mean
		scalar percent=_b[treated]/r(mean)
		outreg2 using ".\Output\Table2-4-panelB.xls", append $outreg2_options cttop(Table2-3)
		
		
		*** Optimal bandwidth, with additional control
		xi:qui: ivregress 2sls `var'  (treated=eligible) runningrl runningrl_inter $control if optimal_bd1, robust	
		sum `var' if e(sample), mean
		scalar percent=_b[treated]/r(mean)
		outreg2 using ".\Output\Table2-4-panelB.xls", append  $outreg2_options  cttop(Table2-3)

		
		*** Optimal bandwidth, with additional control & FE
		xi:qui: ivregress 2sls `var'  (treated=eligible) runningrl runningrl_inter $control $FE if optimal_bd1, robust
		sum `var' if e(sample), mean
		scalar percent=_b[treated]/r(mean)
		outreg2 using ".\Output\Table2-4-panelB.xls", append  $outreg2_options cttop(Table2-3)
	
	}

	
********************************************************************************
	*** Table 4
********************************************************************************
	foreach var of varlist  slfsqw Sel  equsqw Leo  effsqw Sub {
	
		capture drop optimal_bd1
		g optimal_bd1 = (runningrl<`var'_optimal_bd & runningrl>-`var'_optimal_bd)
		
		
		global outreg2_options "lab keep(treated) sortvar(treated eligible) nocons bd(3) sd(3) rd(3) ad(3) adds(Dependent variable mean, r(mean), Percentage difference, percent, Bandwidth, `var'_optimal_bd)"
	
			
		*** Optimal bandwidth, with additional control & FE
		xi:qui: ivregress 2sls `var'  (treated=eligible) runningrl runningrl_inter $control $FE if optimal_bd1, robust
		sum `var' if e(sample), mean
		scalar percent=_b[treated]/r(mean)
		outreg2 using ".\Output\Table2-4-panelB.xls", append  $outreg2_options cttop(Table4)
	
	}
	